package edu.pku.sei.autounit.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.autounit.core.VariableGenerator;

public class Properties {
	
	public static final String VARIABLEGENERATOR = "variableGenerator";
	public static final String MAP_SIZE = "map_size";
	public static final String ARRAY_SIZE = "array_size";
	public static final String COLLECTION_SIZE = "collection_size";
	public static final String BOOLEAN = "boolean";
	public static final String CHAR = "char";
	
	public static final String MAX_SHORT = "max_short";
	public static final String MIN_SHORT = "min_short";

	public static final String MAX_INT = "max_int";
	public static final String MIN_INT = "min_int";
	
	public static final String MAX_LONG = "max_long";
	public static final String MIN_LONG = "min_long";
	
	public static final String MAX_FLOAT = "max_float";
	public static final String MIN_FLOAT = "min_float";
	
	public static final String MAX_DOUBLE = "max_double";
	public static final String MIN_DOUBLE = "min_double";
	
	public static final String NULL = "null";
	
	private static java.util.Properties p ;
	
	static public void loadProperties(File file){
		p = new java.util.Properties();
		try {
			p.load(new FileInputStream(file));
		} catch (Exception e) {
			Log.gotUnexpectedExection("Properties", "static", "", e);
		}
	}
	
	static public double getBoolean(){
		return Double.parseDouble(p.getProperty(BOOLEAN));
	}
	
	static public String getChar(){
		return p.getProperty(CHAR);
	}
	
	static public short getMaxShort(){
		return Short.parseShort(p.getProperty(MAX_SHORT));
	}
	
	static public short getMinShort(){
		return Short.parseShort(p.getProperty(MIN_SHORT));
	}
	
	static public int getMaxInt(){
		return Integer.parseInt(p.getProperty(MAX_INT));
	}
	
	static public int getMinInt(){
		return Integer.parseInt(p.getProperty(MIN_INT));
	}
	
	static public long getMaxLong(){
		return Long.parseLong(p.getProperty(MAX_LONG));
	}
	
	static public long getMinLong(){
		return Long.parseLong(p.getProperty(MIN_LONG));
	}
	
	static public float getMaxFloat(){
		return Float.parseFloat(p.getProperty(MAX_FLOAT));
	}
	
	static public float getMinFloat(){
		return Float.parseFloat(p.getProperty(MIN_FLOAT));
	}
	
	static public double getMaxDouble(){
		return Double.parseDouble(p.getProperty(MAX_DOUBLE));
	}
	
	static public double getMinDouble(){
		return Double.parseDouble(p.getProperty(MIN_DOUBLE));
	}

	public static VariableGenerator[] getVariableGenerators() {
		List<VariableGenerator> list = new ArrayList<VariableGenerator>();
		for(String s:p.getProperty(VARIABLEGENERATOR).split(",")){
			try {
				Class<?> c = Class.forName(s);
				if(VariableGenerator.class.isAssignableFrom(c))
					list.add((VariableGenerator)c.newInstance());
				else
					Log.findBug("Properties", "getVariableFactorys", s+"is not a factory class");
			} catch (ClassNotFoundException e) {
				Log.findBug("Properties", "getVariableFactorys", "unknown factory "+s);
			} catch (InstantiationException e) {
				Log.findBug("Properties", "getVariableFactorys", "the factory should have a no parameter constructor");
			} catch (IllegalAccessException e) {
				Log.findBug("Properties", "getVariableFactorys", "the factory should have a no parameter constructor");
			}
		}
		return list.toArray(new VariableGenerator[0]);
	}
	
	public static double getNull(){
		return Double.parseDouble(p.getProperty(NULL));
	}
	
	public static int getMapSize(){
		return Integer.parseInt(p.getProperty(MAP_SIZE));
	}
	
	public static int getCollectionSize(){
		return Integer.parseInt(p.getProperty(COLLECTION_SIZE));
	}
	
	public static int getArraySize(){
		return Integer.parseInt(p.getProperty(ARRAY_SIZE));
	}
}
